<?php
class PtController extends BaseController
{
    public function __construct($uri)
    {
        parent::__construct($uri);
    }

    public function get()
    {
        $params = array('passkey');
        $this->checkParams($params);
        $tmp = explode('/', $this->uri);
        $filename = substr($tmp['2'], 0, 40);
        $dir = ConfLib::get('torrent', 'torrent_dir');
        $pt_conf = ConfLib::get('torrent', 'pt');
        $bt_encode = new File_Bittorrent2_Encode();
        $bt_decode = new File_Bittorrent2_Decode();
        $bt = $bt_decode->decode(file_get_contents($dir . $filename));
        $bt['announce'] = $pt_conf['tracker'] . '/' . $this->input['passkey'];
        unset($bt['announce-list']);
        $this->output['http']['content-type'] = 'application/x-bittorrent';
        $this->output['data'] = array(
            'filename' => $filename,
            'torrent' => $bt_encode->encode($bt),
        );
        return $this->output;
    }

    public function route()
    {
        if(1 === preg_match('/^\/pt\/[0-9a-z]{32}\.torrent?.+$/', $this->uri)) {
            switch($this->http_method) {
                case 'get' :
                    $tmp = explode('/', $this->uri);
                    return $this->get(substr($tmp['2'], 0,40));
                    break;
            }
        }

        throw new ControllerException();
    }
}
?>
